package com.dormms.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dormms.bean.StaffInfo;
import com.dormms.bean.User;
import com.dormms.bean.UserRole;
import com.dormms.dao.GoodInfoDao;
import com.dormms.dao.StaffInfoDao;
import com.dormms.dao.UserDao;
import com.dormms.dao.UserRoleDao;
import com.dormms.service.StaffInfoService;
@Service
public class StaffInfoServiceImpl implements StaffInfoService {
	@Autowired
	private StaffInfoDao staffInfoDao;
	@Autowired
	private UserDao userDao;
	@Autowired
	private UserRoleDao userRoleDao;
	@Autowired
	private GoodInfoDao goodInfoDao;

	public List<StaffInfo> queryStaffInfoByStaffInfo(StaffInfo staffInfo) {
		// TODO Auto-generated method stub
		return staffInfoDao.queryStaffInfo(staffInfo);
	}

	public Integer insertStaffInfo(StaffInfo staffInfo) {
		String userid = userDao.isUserExist(staffInfo.getStaffId());
		if(staffInfo.getStaffId().equals(userid)) {
			return 0;
		}
		//插入到用户表
		User user = new User();
		user.setUsername(staffInfo.getStaffId());
		user.setPassword("123456");
		userDao.insertUser(user);
		//插入到用户角色表
		UserRole userRole = new UserRole();
		userRole.setUserId(staffInfo.getStaffId());
		userRole.setRoleId(4);
		userRoleDao.insertUserRole(userRole);
		//插入到维修人员表
		staffInfoDao.insertStaffInfo(staffInfo);
		// TODO Auto-generated method stub
		return staffInfo.getStaffInfoId();
	}

	public Integer updateStaffInfoBySuper(StaffInfo staffInfo) {
		// TODO Auto-generated method stub
		return staffInfoDao.updateStaffInfoBySuper(staffInfo);
	}

	public StaffInfo queryStaffInfoByStaffId(String staffId) {
		// TODO Auto-generated method stub
		return staffInfoDao.queryStaffInfoByStaffId(staffId);
	}

	public Integer updateStaffInfo(StaffInfo staffInfo) {
		// TODO Auto-generated method stub
		return staffInfoDao.updateStaffInfo(staffInfo);
	}

	public Integer deleteStaffInfo(String staffId) {
		// TODO Auto-generated method stub
		//删除维修人员分管信息
		goodInfoDao.deleteGoodAdminInfo(staffId);
		//删除维修人员基本信息
		staffInfoDao.deleteStaffInfo(staffId);
		userRoleDao.deleteUserRole(staffId);
		return userDao.deleteUser(staffId);
	}

}
